package com.dahong.cloud.authorize.controller;

import com.dahong.cloud.authorize.domain.SimpleUser;
import com.dahong.cloud.authorize.mapper.SimpleUserMapper;
import com.lambda.security.encoder.StandardPasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    SimpleUserMapper userMapper;

    @Autowired(required = false)
    public void setUserMapper(SimpleUserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @GetMapping("/{id}")
    public SimpleUser getById(@PathVariable String id) {
        return userMapper.selectById(id);
    }

    @PostMapping("/{id}")
    public void save(@PathVariable String id, @RequestBody SimpleUser simpleUser) {

        simpleUser.setUsername(id);
        simpleUser.setPassword(new StandardPasswordEncoder().encode(simpleUser.getPassword()));
        userMapper.insert(simpleUser);
    }

    @PutMapping("/{id}")
    public void update(@PathVariable String id, @RequestBody SimpleUser simpleUser) {
        simpleUser.setUsername(id);
        simpleUser.setNickname("hahah");
        userMapper.updateById(simpleUser);
    }
}
